package com.example.controller;

import com.example.dto.AssignRoleDto;
import com.example.entity.*;
import com.example.page.Page;
import com.example.service.AuthService;
import com.example.service.RoleService;
import com.example.service.UserService;
import com.example.utils.TokenUtils;
import com.example.utils.WarehouseConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:tjf
 * @Description:
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private AuthService authService;
    @Autowired
    private TokenUtils tokenUtils;
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    /**
     * 根据userId获取权限树接口url/user/auth-list
     * @param token 通过token获取当前登录用户的信息
     * @return
     */
    @RequestMapping("/auth-list")
    public Result getAuthList(@RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        // 通过前端归还的Token，获取当前登录的用户信息
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);

        // 获取权限树
        List<Auth> authTree = authService.findAuthTreeByUserId(currentUser.getUserId());
        // 响应
        return Result.ok(authTree);
    }
    /**
     * 获取分页查询用户的接口url/user/user-list
     * @return
     */
    @RequestMapping("/user-list")
    public Result getUserList(Page page, User user){
        userService.queryUserForPagination(page, user);

        return Result.ok(page);
    }

    /**
     * 新增用户接口url/user/addUser
     * @param token
     * @param user
     * @return
     */
    @RequestMapping("/addUser")
    public Result addUser(@RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token,
                          @RequestBody User user){
        // 执行新增用户的业务
        Result result = userService.saveUser(token, user);
        // 返回处理的结果
        return result;
    }

    /**
     * 修改用户状态接口url/user/updateState
     * @param token
     * @param user
     * @return
     */
    @RequestMapping("/updateState")
    public Result updateUserState(@RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token,
                                  @RequestBody User user){
        Result result = userService.modifyUserStateByUserId(user, token);
        return result;
    }

    /**
     * 根据userId查询已分配的角色接口url/user/user-role-list restful风格
     * @param userId
     * @return
     */
    @GetMapping("/user-role-list/{userId}")
    public Result getUserRoleList(@PathVariable("userId") int userId){
        List<Role> roleList = roleService.queryRoleListByUserId(userId);
        return Result.ok(roleList);
    }

    /**
     * 分配用户角色接口url/user/assignRole
     * @return
     */
    @RequestMapping("/assignRole")
    public Result assignUserRole(@RequestBody AssignRoleDto assignRoleDto){
        Result result = roleService.assignRoleByUserId(assignRoleDto);

        return result;
    }

    /**
     * 根据userId删除用户接口url/user/deleteUser/{userId}
     * @param userId
     * @return
     */
    @RequestMapping("/deleteUser/{userId}")
    public Result deleteUserById(@PathVariable("userId") int userId){
        // 执行删除用户的业务方法
        return userService.deleteUserById(userId);
    }

    /**
     * 根据userId集合批量删除用户接口url/user/deleteUserList
     * @return
     */
    @RequestMapping("/deleteUserList")
    public Result deleteUserByList(@RequestBody List<Integer> userIdList){
        // 执行批量删除用户的业务方法
        return userService.deleteUserByList(userIdList);
    }

    /**
     * 根据userId修改UserName的接口url/user/updateUser
     * @param token
     * @param user
     * @return
     */
    @RequestMapping("/updateUser")
    public Result modifyUserNameByUserId(@RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token,
                                         @RequestBody User user){
        // 获取当前用户的信息
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);

        // 二次封装user
        user.setUpdateBy(currentUser.getUserId());

        // 执行修改userName的业务方法
        return userService.modifyUserNameByUserId(user);
    }

    /**
     * 根据userId重置密码接口url/user/updatePwd/{userId}
     * @param userId
     * @param token
     * @return
     */
    @RequestMapping("/updatePwd/{userId}")
    public Result resetPasswdByUserId(@PathVariable("userId") Integer userId,
                         @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        // 获取当前登录用户的信息
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);

        // 创建User对象，并封装参数
        User user = new User();
        user.setUserId(userId);
        user.setUpdateBy(currentUser.getUserId());

        // 执行重置密码的业务方法
        Result result = userService.resetPasswdByUserId(user);
        // 响应
        return result;
    }
}
